package com.gykfbxxh.imovie.bean.news;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.utils.util.ViewSizeUtils;
import com.gykfbxxh.imovie.R;
import com.gykfbxxh.imovie.bean.film.FilmEntity;
import com.gykfbxxh.imovie.bean.film.FilmListEntity;
import com.gykfbxxh.imovie.greendao.GDManager;
import com.gykfbxxh.imovie.utils.glide.ImageLoader;
import com.gykfbxxh.imovie.view.filmsdetail.acitivity.FilmDetailActivity;

import java.util.List;

import static com.gykfbxxh.imovie.base.BaseActivity.PARAMETER1;

/**
 * 列表的一张小图UI
 * Created by xuxinghai on 2017/8/30.
 */
public class ViewScrollView {

    public TextView tv_film_sum;
    public LinearLayout ll_imglistview,ll_scrollview;
    Context context;



    public ViewScrollView(View convertView) {
        initView(convertView);
    }

    public ViewScrollView(View convertView, Context context) {
        this.context = context;
        initView(convertView);
    }

    /**
     * 初始化控件
     * @param convertView
     */
    public void initView(View convertView) {
        ll_scrollview = (LinearLayout) convertView.findViewById(R.id.ll_scrollview);
        tv_film_sum = (TextView) convertView.findViewById(R.id.tv_film_sum);
        ll_imglistview = (LinearLayout) convertView.findViewById(R.id.ll_imglistview);
        convertView.setTag(this);
    }

    /**
     * 加载数据
     * @param listBean
     * @param likeData
     */
    public void loadData(FilmListEntity listBean, final List<String> likeData){
        if(listBean!=null&&listBean.getPicsList()!=null&&listBean.getPicsList().size()>0) {
            List<FilmEntity> picsList = listBean.getPicsList();
            tv_film_sum.setText("全部(200)");
            ll_imglistview.removeAllViews();
            for (int i =0;i<picsList.size();i++){
                final FilmEntity filmEntity = picsList.get(i);

                RelativeLayout relativeLayout = new RelativeLayout(context);
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewSizeUtils.dp2px(context,103), ViewGroup.LayoutParams.MATCH_PARENT);
                relativeLayout.setLayoutParams(layoutParams);
                relativeLayout.setPadding(0,0,ViewSizeUtils.dp2px(context,9),0);
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                final ImageView imageView = new ImageView(context);
                imageView.setLayoutParams(params);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                ImageLoader.getInstance().loadImage1zhi(context,imageView,filmEntity.getFPhoto());
                final int finalI = i;
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, FilmDetailActivity.class);
                        intent.putExtra(PARAMETER1,filmEntity.getFId());
                        context.startActivity(intent);
                    }
                });
                relativeLayout.addView(imageView);

                RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(ViewSizeUtils.dp2px(context,20), ViewSizeUtils.dp2px(context,20));
                params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                final ImageView imageView1 = new ImageView(context);
                imageView1.setLayoutParams(params1);
                imageView1.setPadding(ViewSizeUtils.dp2px(context,4),ViewSizeUtils.dp2px(context,4),ViewSizeUtils.dp2px(context,4),ViewSizeUtils.dp2px(context,4));
                imageView1.setScaleType(ImageView.ScaleType.FIT_XY);
//                imageView1.setImageDrawable(AppApplication.getInstance().getDrawable(R.drawable.collectno));
                imageView1.setImageDrawable(ContextCompat.getDrawable( context,R.drawable.collectno));
//                imageView1.setBackground(AppApplication.getInstance().getResources().getDrawable(R.drawable.likelook_bg));
                imageView1.setBackground(ContextCompat.getDrawable(context,R.drawable.likelook_bg));
                imageView1.setTag(R.id.tag_first,false);
                for (int j = 0;j<likeData.size();j++){
                    if(filmEntity.getFId().equals(likeData.get(j))) {
//                        imageView1.setImageDrawable(AppApplication.getInstance().getDrawable(R.drawable.collect));
                        imageView1.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.collect));
                        imageView1.setTag(R.id.tag_first,true);
                    }
                }
                imageView1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean isSel = (boolean) v.getTag(R.id.tag_first);
                        if(isSel) {
//                            imageView1.setImageDrawable(AppApplication.getInstance().getDrawable(R.drawable.collectno));
                            imageView1.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.collectno));
                            imageView1.setTag(R.id.tag_first,false);
                            likeData.remove(filmEntity.getFId());
                            GDManager.getInstance().delLikeLookData(filmEntity.getFId());
                        }else {
//                            imageView1.setImageDrawable(AppApplication.getInstance().getDrawable(R.drawable.collect));
                            imageView1.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.collect));
                            imageView1.setTag(R.id.tag_first,true);
                            likeData.add(filmEntity.getFId());
                            GDManager.getInstance().insertLikeLookData(filmEntity);
                        }
                    }
                });
                relativeLayout.addView(imageView1);


                ll_imglistview.addView(relativeLayout);
            }

            ll_scrollview.setVisibility(View.VISIBLE);
        }else {
            ll_scrollview.setVisibility(View.GONE);
        }


    }


//    private void collectData(FilmEntity filmEntity) {
//
//        if(filmEntity==null) {
//            return;
//        }
//
//        ParamMap paramMap = new ParamMap();
//        paramMap.put("classtype","1");
//        paramMap.put("apptype","4");
//        paramMap.put("objectid",filmEntity.getFId());
//        HttpUtils.addPostResultRequest(URIContant.ADDCOLLECTLOG, "addCollect", paramMap, new HttpListener<String>() {
//            @Override
//            public void onError(VolleyError error) {
//
//            }
//
//            @Override
//            public void onSuccess(String response) {
//
//            }
//        });
//
//
//    }
}
